package com.itheima.ydd.commom;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @autgor:swx
 * @create:2022-12-26-19-48
 * @Description:用户分页查询结果
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult implements Serializable {
    private Long count;//总条数
    private Long totalPages;//总页数
    private Long pageSize;//每页条数
    private Long currentPage;//当前页
    private Object data;//数据

    public static PageResult init(Long count, Long pageSize, Long currentPage, List data){
        long totalPage = count % pageSize == 0 ? count/pageSize : count/pageSize + 1;

        return new PageResult(count, currentPage, pageSize, totalPage, data);
    }

    public static PageResult init(Page page){

        long totalPage = page.getTotal() % page.getSize() == 0 ? page.getTotal()/page.getSize() : page.getTotal()/page.getSize() + 1;
        return new PageResult(page.getTotal(), page.getCurrent(), page.getSize(), totalPage, page.getRecords());
    }
}
